package com.lqk.demo.pessimisticlock.syncmode.sequencecontrol.fixedoperatesequence;

import lombok.extern.slf4j.Slf4j;

/**
 * 固定运行顺序：WaitNotify
 * @author lqk
 * @date 2021/12/23 15:40
 */
@Slf4j
public class TestWaitNotify {
    static final Object lock = new Object();
    // 表示t1是否运行过
    static boolean t1runned = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock){
                log.debug("1");
                t1runned = true;
                lock.notifyAll();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (lock){
                while (!t1runned){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("2");
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
